package net.sos.services.impl;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import net.sos.services.common.ICommonConstants;
import net.sos.services.contract.IDropDownOptionsService;
import net.sos.services.valueobject.ConfigParameter;
import net.sos.services.valueobject.MapDataType;
import net.sos.services.valueobject.MapEntry;

@WebService(serviceName = "DropDownOptionsImpl", targetNamespace = ICommonConstants.TARGET_NAMESPACE, endpointInterface = "net.sos.services.contract.IDropDownOptionsService")
public class DropDownOptionsImpl implements IDropDownOptionsService {

   static {
      System.out.println("LOADED!!!!!");
      java.net.URL [] urls = ((java.net.URLClassLoader)Thread.currentThread().getContextClassLoader()).getURLs();
      for(URL url : urls) {
         System.err.println(url);
      }
      final ApplicationContext ctx = new ClassPathXmlApplicationContext("A.xml");
   }
   
   /* (non-Javadoc)
    * @see net.sos.services.contract.IDropDownOptionsService#getDropDownOptions(java.util.List)
    */
   @Override
   public MapDataType getDropDownOptions(List<ConfigParameter> inputList) {
      MapDataType map = new MapDataType();
      if (inputList == null || inputList.size() == 0) {
         return map;
      }
      List<MapEntry> entryList = new ArrayList<MapEntry>();
      for (ConfigParameter inputConfig : inputList) {
         MapEntry mapEntry = new MapEntry();
         mapEntry.setKey(inputConfig);
         mapEntry.setValueList(getOutList(inputConfig));
         entryList.add(mapEntry);
      }
      map.setEntryList(entryList);
      return map;
   }

   private List<ConfigParameter> getOutList(ConfigParameter inputConfig) {
      List<ConfigParameter> outList = new ArrayList<ConfigParameter>();
      
      ConfigParameter outputConfig = new ConfigParameter();
      outputConfig.setMasterCode(inputConfig.getMasterCode());
      outputConfig.setDescription("MY-DESC-" + Math.random());
      outputConfig.setSubCode("MY-VALUE-" + Math.random());
      outList.add(outputConfig);
      
      outputConfig = new ConfigParameter();
      outputConfig.setMasterCode(inputConfig.getMasterCode());
      outputConfig.setDescription("MY-DESC-" + Math.random());
      outputConfig.setSubCode("MY-VALUE-" + Math.random());
      outList.add(outputConfig);
      
      return outList;
   }

}
